/**
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Old developer team
 * @version 1.1
 */

/*
WADSInitOrg Class is Called From "MainAPP"
It use to manage Organization file.
Call this Window First When Application Start.

Application will ask user, what do you want to do by
choosen 3 choices that be prepared then do the things
user order.
*/

package wads.app;

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import wads.com.WADTools;

@Deprecated
public class WADInitOrg extends JFrame {
//  private static final long serialVersionUID = 6053272292959891348L;
  
  private WADDiagramEditor wad;
  private UserManagerFrame umf;
  private JPanel contentPane;
  private JButton okButton = new JButton("OK");
  private JButton cancelButton = new JButton("Cancel");
  private JPanel jPanel1 = new JPanel();
  private JRadioButton newOrg = new JRadioButton("New Organize", true);
  private JRadioButton openOrg = new JRadioButton("Open Organize", false);
  private JRadioButton manageOrg = new JRadioButton("Manage Organize", false);
  private JLabel textLabel = new JLabel("Choose one...");
  private JLabel pic = new JLabel(new ImageIcon("images/book.gif"));

  private ButtonGroup group = new ButtonGroup();
  private JList jList1 = new JList();
  private JScrollPane jScrollPane1 = new JScrollPane();

  /**Construct the frame*/
  public WADInitOrg() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  /**Component initialization*/
  private void jbInit() throws Exception {
    newOrg.addMouseListener(new MouseAdapter() {
            @Override
      public void mousePressed(MouseEvent e) {
        newOrg_mousePressed(e);
      }
    });

    openOrg.addMouseListener(new MouseAdapter() {
            @Override
      public void mousePressed(MouseEvent e) {
        openOrg_mousePressed(e);
      }
    });

    manageOrg.addMouseListener(new MouseAdapter() {
            @Override
      public void mousePressed(MouseEvent e) {
        manageOrg_mousePressed(e);
      }
    });

    jList1.setBorder(BorderFactory.createLoweredBevelBorder());
    jList1.setBounds(new Rectangle(3, 74, 189, 113));
    textLabel.setForeground(Color.black);
    textLabel.setBounds(new Rectangle(20, 12, 75, 23));

    newOrg.setBounds(new Rectangle(3, 0, 100, 30));
    newOrg.addActionListener(new java.awt.event.ActionListener() {
            @Override
      public void actionPerformed(ActionEvent e) {
        newOrg_actionPerformed(e);
      }
    });

    openOrg.setBounds(new Rectangle(3, 24, 120, 30));
    okButton.setBounds(new Rectangle(219, 149, 78, 29));
    cancelButton.setBounds(new Rectangle(218, 191, 79, 28));
    jPanel1.setBounds(new Rectangle(11, 29, 195, 192));

    pic.setForeground(Color.black);
    pic.setBounds(new Rectangle(220, 44, 82, 82));

    jScrollPane1.setBounds(new Rectangle(3, 74, 189, 113));
    manageOrg.setBounds(new Rectangle(3, 45, 120, 30));

    group.add(newOrg);
    group.add(openOrg);
    group.add(manageOrg);

    okButton.addActionListener(new java.awt.event.ActionListener() {
            @Override
    public void actionPerformed(ActionEvent e) {
        okButton_actionPerformed(e);
      }
    });

    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(null);
    this.setResizable(false);
    this.setSize(320, 300);
    this.setTitle("Organize Selection");

    cancelButton.addActionListener(new ActionListener() {
            @Override
      public void actionPerformed(ActionEvent e) {
        cancelButton_actionPerformed(e);
      }
    });

    jPanel1.setBorder(BorderFactory.createEtchedBorder());
    jPanel1.setLayout(null);

    contentPane.add(jPanel1, null);
    jPanel1.add(jList1, null);
    jPanel1.add(jScrollPane1, null);
    jPanel1.add(newOrg, null);
    jPanel1.add(manageOrg, null);
    jPanel1.add(openOrg, null);

    contentPane.add(okButton, null);
    contentPane.add(cancelButton, null);
    contentPane.add(pic, null);
    contentPane.add(textLabel, null);
  }

  /*Overridden so we can exit when window is closed*/
    @Override
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  private void cancelButton_actionPerformed(ActionEvent e) {
      System.exit(0);
  }

  private void okButton_actionPerformed(ActionEvent e) {
    String org, input_buffer;
    File f;
    boolean checkFileExist = true;
    if((wad == null && (!manageOrg.isSelected()))){
      if(jList1.isSelectionEmpty() && newOrg.isSelected()){
        try{
          do{
            input_buffer = JOptionPane.showInputDialog(this,"Enter Organization name ");
            org = input_buffer + ".org";
            f = new File(WADTools.POWER_USER_PATH + org);
            checkFileExist = f.exists();
            if (checkFileExist && input_buffer != null){
              JOptionPane.showMessageDialog(this, "Error!?",
                "Error with Duplicate File Name", JOptionPane.WARNING_MESSAGE);
            }
            else if(input_buffer == null){
              System.out.println("Error Type Input");
            }
            else{
              f.createNewFile();
//              WADTools.createNewOrg(f);
//              WADTools.createNewRestrictUserFile(true);
              umf = new UserManagerFrame(null,org);
              wad = new WADDiagramEditor(org);
              wad.setSize(600,400);
              wad.setVisible(true);
              umf.setVisible(true);
              this.setVisible(false);
              checkFileExist = false;
            }
          }while(checkFileExist);

        }catch(Exception ex){}
      }else if(openOrg.isSelected()&& !jList1.isSelectionEmpty()){
        wad = new WADDiagramEditor(jList1.getSelectedValue().toString());
        wad.setSize(600,400);
        wad.setVisible(true);
        this.setVisible(false);
      }
    }else if(manageOrg.isSelected() && (jList1.getSelectedIndex() != -1)){
//         WADTools.createNewRestrictUserFile(true);
         umf = new UserManagerFrame(null,jList1.getSelectedValue().toString());
         umf.setVisible(true);
    }else{
      // Nothing Special
    }
  }

  private void newOrg_mousePressed(MouseEvent e) {
    if(!newOrg.isSelected()){
      String[] tmp = {""};
      jList1.setListData(tmp);
    }
  }

  private void openOrg_mousePressed(MouseEvent e) {
    String ext = null;
    Vector<String> org = new Vector<String>();
    File dirOrg = new File("./PowerUser");

    File[] listOrg = dirOrg.listFiles();
    for(int i=0; i<listOrg.length;i++){
      if(listOrg[i].isFile()){
        int index = listOrg[i].toString().lastIndexOf(".");
        ext = listOrg[i].toString().substring(index+1);
        if((ext.compareTo("org") == 0)){
          int indexOrg = listOrg[i].toString().lastIndexOf("\\");
          org.add(listOrg[i].toString().substring(indexOrg+1));
        }
      }
    }

    jList1.setListData(org);
    jScrollPane1.setViewportView(jList1);
  }

  private void newOrg_actionPerformed(ActionEvent e) {
        String[] tmp = {""};
        jList1.setListData(tmp);
  }

  private void manageOrg_mousePressed(MouseEvent e) {
    String ext = null;
    Vector<String> org = new Vector<String>();
    File dirOrg = new File("./PowerUser");

    File[] listOrg = dirOrg.listFiles();
    for(int i=0; i<listOrg.length;i++){
      if(listOrg[i].isFile()){
        int index = listOrg[i].toString().lastIndexOf(".");
        ext = listOrg[i].toString().substring(index+1);
        if((ext.compareTo("org") == 0)){
          int indexOrg = listOrg[i].toString().lastIndexOf("\\");
          org.add(listOrg[i].toString().substring(indexOrg+1));
        }
      }
    }

    jList1.setListData(org);
    jScrollPane1.setViewportView(jList1);
  }
}